using System;
using Glue;
using Xunit;

namespace GlueTests.MapperTests
{
    public class When_mapping_a_flattened_towardsright_relation
    {
        private readonly Mapping<DomainPerson, GuiPerson> personMapping;
        private const string cityName = "Flattened City";

        public class GuiPerson
        {
            public String Name { get; set; }
            public String City { get; set; }
        }

        public class DomainPerson
        {
            public String Name { get; set; }
            public DomainAddress Address { get; set; }
        }

        public class DomainAddress
        {
            public String City { get; set; }
        }

        public When_mapping_a_flattened_towardsright_relation()
        {
            personMapping = new Mapping<DomainPerson, GuiPerson>();
            var addressMapping = new Mapping<DomainAddress, GuiPerson>();
            addressMapping.AutoRelateEqualNames();
            personMapping.Flatten(domain => domain.Address, addressMapping);
        }

        [Fact]
        public void Should_map_towards_right()
        {
            var domainPerson = new DomainPerson { Address = new DomainAddress { City = cityName } };
            var guiperson = personMapping.Map(domainPerson, new GuiPerson());
            Assert.Equal(cityName, guiperson.City);
        }

        [Fact]
        public void Should_map_towards_left()
        {
            var domainPerson = new DomainPerson { Address = new DomainAddress() };
            var guiperson = new GuiPerson {City = cityName};
            personMapping.Map(guiperson, domainPerson);
            Assert.Equal(cityName, domainPerson.Address.City);
        }
    }
}